package com.bien.springcloud.zuul.filter;

import com.bien.springcloud.util.RequestUtil;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求路由之前过滤器
 */
@Component
public class AccessPreFilter extends ZuulFilter {

	private static Logger log = LoggerFactory.getLogger(AccessPreFilter.class);

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public Object run() {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		String resquestURI = request.getRequestURI();
		log.info("[###日志记录###]" + RequestUtil.getRemoteHost(request) + "在 " + RequestUtil.getCurrentTime() + " 访问："
				+ resquestURI + ",请求方式：" + request.getMethod());

		return null;
	}

}
